package com.yunc.modules.sys.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.common.queue.activemq.ActiveMQSender;
import com.yunc.common.utils.BaseConstant;
import com.yunc.common.utils.R;
import com.yunc.common.utils.ZTreeNode;
import com.yunc.modules.sys.entity.SysUser;
import com.yunc.modules.sys.mapper.SysUserMapper;
import com.yunc.modules.sys.service.ISysUserRoleService;
import com.yunc.modules.sys.service.ISysUserService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户信息
 *
 * @author taocongcong
 * @create 2017-12-15 13:46
 */
@Service("sysUserService")
@CacheConfig(cacheNames = "sysUser")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper,SysUser> implements ISysUserService {

    @Autowired
    private ActiveMQSender activeMQSender;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    public List<String> queryMenuIdByUserId(Long userId) {
        return sysUserMapper.queryMenuIdByUserId(userId);
    }

    @Override
    public Set<String> queryUserMenuPermsList(Long userId) {
        List<String> permsList = sysUserMapper.queryUserMenuPermsList(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : permsList) {
            if (StringUtils.isNotEmpty(perm)){
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public List<String> queryMenuIdByRoleId(Long roleId) {
        return sysUserMapper.queryMenuIdByRoleId(roleId);
    }

    @Override
    public IPage<SysUser> pageInfo(Page<SysUser> page, SysUser sysUser) {
        // 分页
        return sysUserMapper.pageInfo(page, sysUser.getUserName(), sysUser.getDeptCode());
    }

    @Override
    @Cacheable(value = "user-dept-tree-key")
    public List<ZTreeNode> listDeptUserTree() {
        return sysUserMapper.listDeptUserTree();
    }

    @Override
    public R saveUser(SysUser user) {
        // 设置默认密码
        String password = DigestUtil.md5Hex("123456");
        user.setUserPassword(password);
        user.setRemoveTag(BaseConstant.REMOVE_N);
        boolean flag = this.save(user);
        if (flag) {
            // 修改权限
            boolean userRoleFlag = sysUserRoleService.insertList(user.getId(), user.getRoleIds());
            if (userRoleFlag) {
                /**
                 * 发送消息初始化该用户的chat
                 */
                Destination destination = new ActiveMQQueue("chat.friend.group.create.queue");
                activeMQSender.sendChannelMess(destination, String.valueOf(user.getId()));
                return R.ok();
            }
        }
        return R.error();
    }
}
